package controller;

import DataAcess.Bus;
import DataAcess.BusReader;
import DataAcess.Place;
import Interface.MainGui;
import java.awt.Color;
import java.util.ArrayList;


/*
 * Class ID	: SE715
 * Student ID	: 02967
 * Student Name : Vu Ngoc Trung
 * Due Date     : Jul 24, 2013
 * I declare that this assignment is my own work in accordance with FPT Policy.
 */
public class SearchFamousController implements controller.Controller {

    //Access List all form and array list contant all bus object
    private MainGui form;
    ArrayList<Bus> arrbus;
    ArrayList<Place> arrfamous;
    //access controller that relationship with MainController
    controller.Controller controller;
    
    public SearchFamousController(ArrayList arrbus, controller.Controller controller, MainGui mgui) {
        this.arrbus = arrbus;
        arrfamous = new BusReader("famous.txt").arrpPlaces();
        this.controller = controller;
        form = mgui;
    }
    
    @Override
    public void handleEvent(String event) {
        if (event.equals("show all of type")) {
            form.cbLocationSuggest.removeAllItems();
            for (int i = 0; i < arrfamous.size(); i++) {
                if (form.cbtypeSuggest.getSelectedItem().equals(arrfamous.get(i).getType().trim())) {
                    form.cbLocationSuggest.addItem(arrfamous.get(i).getName());
                }
            }
        }

        if(event.equals("ViewBus")){
            ViewBus();
        }
        if (event.equals("show suggestions")) {
            String s = null;
            for (Place place : arrfamous) {
                if (place.getName().equals(form.cbLocationSuggest.getSelectedItem())) {
                    s = place.getBusPassing().trim();
                }
            }
            String[] s2;
            s2=s.split(" ");

            int[] a = new int[s2.length];
            
            for (int i = 0; i < s2.length; i++) {
                try {
                    a[i]=Integer.parseInt(s2[i]);
                } catch (Exception e) {
                }
            }
            for (int i = 0; i < 50; i++) {
                form.tbSuggest.setValueAt("", i, 0);
                form.tbSuggest.setValueAt("", i, 1);
            }
            for (int i = 0; i < a.length; i++) {
                for (Bus bus : arrbus) {
                    if (a[i] == bus.getNumber()) {
                        form.tbSuggest.setValueAt(bus.getNumber(), i, 0);
                        form.tbSuggest.setValueAt(bus.getName(), i, 1);
                    }
                }
            }
        }
        
    }
        public void view(Bus bus) {
        form.lbNameSuggest.setText(bus.getName());
        form.lbNoSuggest.setText(Integer.toString(bus.getNumber()));
        form.taGoSuggest.setText(bus.getGo());
        form.taBackSuggest.setText(bus.getBack());
        form.taGoSuggest.setEditable(false);
        form.taBackSuggest.setEditable(false);
    }

    public void ViewBus() {
        int mouseSelectedIndex = form.tbSuggest.getSelectedRow();

        if (!(form.tbSuggest.getValueAt(mouseSelectedIndex, 0).equals(""))) {
            Bus bus = null;
            for (Bus bus1 : arrbus) {
                if (bus1.getNumber() == form.tbSuggest.getValueAt(mouseSelectedIndex, 0)) {
                    bus = bus1;
                }
            }
            view(bus);
        }
    }
}